package univer.oko.search.queryBuilders;

import univer.oko.search.searchParamsMaps.ISearchParamsMap;
import univer.oko.search.searchParamsMaps.StudentWithTotalProblemsSearchParamsMap;

import java.util.Set;

/**
 * Created by Sergey Kharlamov.
 * Date: 30.09.2006
 * Time: 10:49:31
 */
public class StudentsWithTotalProblemsQueryBuilder implements IQueryBuilder {
    private StringBuilder builder;

    public String createQuery(ISearchParamsMap map) {
        builder = new StringBuilder();
        Set<String> keys = map.getKeys();
        for (String param : keys) {
            ProcessMapping(param, map.getValue(param), builder);
        }
        if (builder.length() != 0) {
            builder.delete(builder.length() - "AND ".length(), builder.length());
        }
        return builder.toString();
    }

    private void ProcessMapping(String key, Object value, StringBuilder builder) {
        if (key.equals(StudentWithTotalProblemsSearchParamsMap.STUDENT_FIRST_NAME)) {
            builder.append("[studname] like ('" + value + "%')");
        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.STUDENT_LAST_NAME)) {
            builder.append("[studsurname] like ('" + value + "%')");
        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.FACULTY_NAME)) {
            builder.append("[Faculty_Name] =  '" + value + "'");
        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.DIRECTION_NAME)) {
            builder.append("[Direction_Name] =  '" + value + "'");
        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.SPECIALITY_NAME)) {
            builder.append("[Speciality_Name] =  '" + value + "'");
        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.IS_UKRAINIAN)) {
            if (value.equals((ISearchParamsMap.TRUE))) {
                builder.append("[ref_country] = 1");
            } else {
                builder.append("[ref_country] <> 1");
            }

        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.EDUCATION_FORM_NAME)) {
            builder.append("[EducForm_Name] =  '" + value + "'");
        } else if (key.equals(StudentWithTotalProblemsSearchParamsMap.REF_EDUCATION_FORM)) {
            builder.append("[ref_educForm] = " + value);
        }/* else if (key.equals(StudentWithTotalProblemsSearchParamsMap.REF_EDUCATION_FORM)) {
            builder.append("[ref_speciality] = " + value);
        }*/
        builder.append(" AND ");
    }

}
